package com.gopay.bis.manager.bank.ebank;

import java.util.Map;

import javax.annotation.Resource;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.tccb.ebank.TccbB2cMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BankManagerRemoteService;
import com.gopay.remote.bank.businesses.BankService;

public class TccbB2cManager implements BankManagerRemoteService {

	@Resource(name = "tccbB2cBankServiceClientHna")
	private BankService tccbB2cBankServiceClientHna;

	@Resource(name = "tccbB2cBankServiceClient")
	private BankService tccbB2cBankServiceClient;
	
	@Resource(name = "tccbConfig")
	private Map<String, String> tccbConfig;

	private static final String SEPARATOR = "~|~";

	/**
	 * 支付交易单笔查询
	 */
	@Override
	public Object query(Object baseMessage) throws GopayException {
		TccbB2cMessage message = (TccbB2cMessage) baseMessage;
		message.setTransName((String) tccbConfig.get("TCCB.B2C.QUERY.TRANSNAME"));
		message.setMerchantId(message.getMerID());
		message.setMerchantUrl(message.getPayRuturnUrl());
		buildPlain4Query(message);
		BankLogUtils.printB2CManagerQuerySendLog(message, message);
		if(message.isHna()){
			message = (TccbB2cMessage)tccbB2cBankServiceClientHna.query(message);
		}else{
			message = (TccbB2cMessage)tccbB2cBankServiceClient.query(message);
		}
		message.setOutStlmCd(BankConstants.BANK_LOGO_TCCB);
		BankLogUtils.printB2CManagerQueryReturnLog(message);
		return message;
	}

	/**
	 * 支付加签
	 */
	@Override
	public Object sign(BaseMessage baseMessage) throws GopayException {
		TccbB2cMessage message = (TccbB2cMessage) baseMessage;
		message.setTransName(tccbConfig.get("TCCB.B2C.TRANSNAME"));
		message.setMerchantId(message.getMerID());		
		message.setTermCode(tccbConfig.get("TCCB.B2C.TERMCODE"));
		message.setMercUrl(message.getPayRuturnUrl());
		message.setBankUrl(message.getPayUrl());	
		buildPlain4Pay(message);
		BankLogUtils.printB2CManagerSignSendLog(message);
		String signStr;
		if(message.isHna()){
			signStr = (String) tccbB2cBankServiceClientHna.sign(message);
		}else{
			signStr = (String) tccbB2cBankServiceClient.sign(message);
		}
		message.setSignMessage(signStr);
		BankLogUtils.printB2CManagerSignReturnLog(message);
		return message;
	}

	/**
	 * 支付验签
	 */
	@Override
	public Object verify(BaseMessage baseMessage) throws GopayException {
		TccbB2cMessage message = (TccbB2cMessage) baseMessage;
		BankLogUtils.printB2CManagerVerifySendLog(message);
		Boolean isVerify;
		if(message.isHna()){
			isVerify = (Boolean)tccbB2cBankServiceClientHna.verify(message);
		}else{
			isVerify = (Boolean)tccbB2cBankServiceClient.verify(message);
		}
		BankLogUtils.printB2CManagerVerifyReturnLog(message,isVerify);
		return isVerify;
	}

	/**
	 * 拼装支付明文参数
	 * @param message
	 */
	public void buildPlain4Pay(TccbB2cMessage message) {
		StringBuffer sb = new StringBuffer();
		sb.append("TransactionName=").append(message.getTransName())
		.append(SEPARATOR).append("MercDtTm=")
		.append(message.getOrderDate()).append(SEPARATOR)
		.append("MerchantSeqNo=").append(message.getOrderId())
		.append(SEPARATOR).append("MerchantId=")
		.append(message.getMerchantId()).append(SEPARATOR)
		.append("TermCode=").append(message.getTermCode())
		.append(SEPARATOR).append("TranAmt=")
		.append(message.getAmount()).append(SEPARATOR)
		.append("Remark1=").append(message.getRemark1())
		.append(SEPARATOR).append("Remark2=")
		.append(message.getRemark2()).append(SEPARATOR)
		.append("MercUrl=").append(message.getMercUrl());
		message.setPlainMessage(sb.toString());
	}

	/**
	 * 拼装查询明文参数
	 * @param message
	 */
	public void buildPlain4Query(TccbB2cMessage message){		
		StringBuffer sb = new StringBuffer();
		sb.append("TransName=").append(message.getTransName())
		.append(SEPARATOR).append("MerchantId=")
		.append(message.getMerchantId()).append(SEPARATOR)
		.append("MerchantSeqNo=").append(message.getOrderId())
		.append(SEPARATOR).append("MerchantUrl=")
		.append(message.getMerchantUrl());
		message.setPlainMessage(sb.toString());
	}

	@Override
	public Object dayend(BaseMessage base) throws GopayException, Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object refund(BaseMessage base) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	
}
